package com.zsguan.z.meixi;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.zsguan.s.stools4andr.base.BaseActivity;
import com.zsguan.s.stools4andr.os.ZFileScanTask;

import java.io.File;
import java.lang.ref.WeakReference;

/**
 * Created by Administrator on 2015/9/14.
 * File create update delete listener
 */
public class ZFileCudActivity extends BaseActivity {

    private static final String LOCAL_FOLDER_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "zfolder1";

//    private SFileCudListener mFileCudListener;

    private ZFileScanTask mFileScanTask;

    private TextView file_cud_info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_cud);

        file_cud_info = (TextView) findViewById(R.id.file_cud_info);

        mFileScanTask = new ZFileScanTask(new ScanHandler(ZFileCudActivity.this));
//        mFileCudListener = new SFileCudListener(Environment.getExternalStorageDirectory().getAbsolutePath());

//        mFileCudListener.startWatching();
    }

    @Override
    protected void onDestroy() {
//        mFileCudListener.stopWatching();

        super.onDestroy();
    }

    public void delete(View view) {
        File file = new File(LOCAL_FOLDER_PATH);
        if (file.exists() && !file.delete()) {
            toast(getString(R.string.delete) + getString(R.string.failed));
        }
    }

    public void scan(View view) {
        if (mFileScanTask.isRunning()) {
            mFileScanTask.stop();
        } else {
            mFileScanTask.start();
        }
    }

    public void create(View view) {
        File file = new File(LOCAL_FOLDER_PATH);
        if (!file.exists() && !file.mkdirs()) {
            toast(getString(R.string.create) + getString(R.string.failed));
        }
    }

    private void append(String message) {
        file_cud_info.append(message);
        file_cud_info.append("\r\n");
    }

    public void clear(View view) {
        mFileScanTask.clear();
        file_cud_info.setText("");
    }

    static class ScanHandler extends Handler {
        WeakReference<Activity> mActivityWeakReference;

        public ScanHandler(Activity activity) {
            this.mActivityWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            ZFileCudActivity activity = (ZFileCudActivity) mActivityWeakReference.get();
            String info = "";

            switch (msg.what) {
                case -1:
                    info = "初始化文件列表";
                    break;
                case 0:
                    info = "开始扫描";
                    break;
                case 1:
                    info = "结束扫描\r\n共扫描文件 " + msg.obj + "个\r\n\r\n--------------------------------------------\r\n";
                    break;
                case 101:
                    info = "修改：" + String.valueOf(msg.obj);
                    break;
                case 102:
                    info = "创建：" + String.valueOf(msg.obj);
                    break;
            }

            activity.append(info);
        }
    }
}
